<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/permission/edit.css"/>

<div class="permission">
    <div class="edit">
        <?php echo form_open('permission/save/'.$memberID, array('class' => 'form-search', 'id' => "edit_permission")) ?>
        <!--<form id="edit_permission" method="post" class = "form-search">-->
        <?php
        //echo $permissions;
        //echo count($permissions);
        foreach($permissions as $permission)
        {
            $isPermitted = '';
            $permissionName = $permission['Permissions'];
            foreach($permissionsList as $per)
            {
                $perName = getPermissionName($permissions, $per['PermissionID']);
                if($perName == $permissionName)
                {
                    $isPermitted = 'checked';
                    break;
                }
            }
            echo '<input type="checkbox" id="'.$permissionName.'" name="'.$permissionName.'" value="true" '.$isPermitted.'/> '.$permissionName.'<br/>';

        }
            ?>
        <? echo form_close()?>
    </div>
</div>

<?php
function getPermissionName($permissions, $id)
{
    foreach($permissions as $permission)
    {
        if($permission['ID'] == $id)
            return $permission['Permissions'];
    }
}
?>
<div class="growUI"></div>

<script>
    $('#edit_permission').on('submit', function(e){
        e.preventDefault();

        $.ajax({
            //this is the php file that processes the data and send mail
            url: "<?php echo base_url();?>permission/save/<?php echo $memberID?>",
            //GET method is used
            type: "POST",
            data: $('#edit_permission').serialize(),
            //success
            dataType: "json",
            success: function (data, $textStatus, $asdf) {
                console.log(data);

                if(data.status)
                    $.growlUI('Success', 'Permission Edited!');
                $( "#edit-permission-form" ).dialog( "close" );
            },
            error: function (data, textStatus, errorThrown) {
                console.log(data);
                console.log('error');
            }
        });

    });
</script>